<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Transportoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('transportoption');
	}
	public function ajax_transportoption_get(){
		$transportoptionEntity =  new CI_Transport_option_entity();
		$transportoption = $transportoptionEntity->getTransportOptionByCode($_REQUEST['code']);
		echo json_encode($transportoption);
	}
	public function ajax_transportoption_list(){
		$transportoptionEntity =  new CI_Transport_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$transportoptionList = $transportoptionEntity->getTransportOptionListByLanguage($defaultLanguageId);
		$data['transportoptionList'] = $transportoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/transportoptions/index', $data);
	}
	public function ajax_transportoption_save(){
		$transportoptionNameList = $_REQUEST['transportoption'];
		$transportoptionNameDefault = '';
		foreach ($transportoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$transportoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($transportoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_transportoption_require');
		}else{
			$code = 0;
			foreach ($transportoptionNameList as $key => $value) {
				$transportoptionEntity =  new CI_Transport_option_entity();
				if($value != null && $value != ''){
					$transportoptionEntity->setTransport($value);
				}else{
					$transportoptionEntity->setTransport($transportoptionNameDefault);
				}
				$transportoptionEntity->setLanguageId($key);
				$transportoptionEntity->setCode($code);
				$id = $transportoptionEntity->save();
				if($code == 0){
					$code = $id;
					$transportoptionEntity->setId($id);
					$transportoptionEntity->setCode($code);
					$transportoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_transportoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_transportoption_update(){
		$transportoptionNameList = $_REQUEST['transportoption'];
		$transportoptionIdList = $_REQUEST['transportoption_id'];
		$transportoptionCode = $_REQUEST['transportoption_code'];
		$transportoptionNameDefault = '';
		foreach ($transportoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$transportoptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($transportoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_transportoption_require');
		}else{
			foreach ($transportoptionNameList as $key => $value) {
				$transportoptionEntity =  new CI_Transport_option_entity();
				if($value != null && $value != ''){
					$transportoptionEntity->setTransport($value);
				}else{
					$transportoptionEntity->setTransport($transportoptionNameDefault);
				}
				$transportoptionEntity->setLanguageId($key);
				$transportoptionEntity->setCode($transportoptionCode);
				$transportoptionEntity->setId($transportoptionIdList[$key]);
				$transportoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_transportoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_transportoption_delete(){
		$result = array();
		$transportoptionEntity =  new CI_Transport_option_entity();
		$result['result'] = $transportoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_transportoption_delete_success');
		}else{
			$result['message'] = lang('escort_transportoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>